{include file="public/link"}
<body>
    <ul class="edittask">
        <dl><h5>{:__('任务分类')}：</h5>
            <select id="cat_id_1" type="text" class="layui-input"><option value="0">{:__('请选择分类')}</option></select>
            <select id="cat_id" type="text" class="layui-input"></select>
        </dl>
        <dl><h5>{:__('任务标题')}：</h5><input id="title" type="text" class="layui-input"/></dl>
        <dl><h5>{:__('任务内容')}：</h5>{include file="public/editor"}</dl>
        <dt class='attach_edit'>
            <span>{:__('任务要求太多，我写在附件里了')}</span>
            <p class="enclosure"><a class='click_enclosure' style="color: red;" id="click_enclosure">{:__('点击添加附件')}</a>
                    <span class='enclosure_rule'>({:__('允许上传附件格式')}:.xls,.xlsx,.doc,.docx,.zip,.rar,.jpg,.png,.gif .{:__('上限')}10M)</span></p>
            <p class="attach_p">
                <input style="display: none;" id="attach" class="attach layui-btn layui-btn-primary" type="file"/>
                <button id="attach_copy" class="layui-btn layui-btn-primary">{:__('选择文件')}</button><span id="file_name">{:__("请选择上传的文件")}</span>
                <button style="display: none;" id="attach_sub" class="attach_sub layui-btn layui-btn-primary">{:__('上传')}</button>
            </p>
        </dt>
        <dl>
            <button id="edit_task" class="layui-btn layui-btn-primary">{:__('保存')}</button>
        </dl>
    </ul>
</body>
<script>
    noLog();
    var id = "{$id}";
    if(id == '0'){
        layer.msg("{:__('获取id失败')}",{icon:3},function(){
            parent.location.reload();
        });
    }
    var htl = "";
    var mission_category = getLocalStorage('mission_category');
    function getCat()
    {
        mission_category = getLocalStorage('mission_category');
        mission_categoryObj = JSON.parse(mission_category);
        for(i in mission_categoryObj){
            htl += '<option value="'+mission_categoryObj[i]['id']+'">'+mission_categoryObj[i]['name']+'</option>';
        }
        $("#cat_id_1").append(htl);
        $(document).dequeue('task_class');
    }
    // 获取分类及获取编辑
    $(document).queue('task_class',function(){
        if(mission_category != null && mission_category != '' && mission_category != undefined){
            getCat();
        }  else {
            var objs = {
                'mission_category':1
            }
            CacheLoad(objs);
            $(document).queue("mission_category,",function(){
                getCat();
            });
        }
    });
    $(document).queue('task_class',function(){
        
        $.ajax({
            url:'/api/mission/detail',
            type:'get',
            data:{'id':"{$id}"},
            success:function(res){
                if(res.code == 1){
                    ret = res['data'];
                    if(ret['pid'] != '' && ret['pid'] != null  && ret['pid'] != undefined){
                        $("#cat_id_1").val(ret['pid']);
                        $("#cat_id_1").trigger("click");
                    }
                    if(ret['cat_id'] != '' && ret['cat_id'] != null  && ret['cat_id'] != undefined){
                        $("#cat_id").val(ret['cat_id']);
                    }
                    if(ret['title'] != '' && ret['title'] != null  && ret['title'] != undefined){
                        $("#title").val(ret['title']);
                    }
                    if(ret['content'] != '' && ret['content'] != null  && ret['content'] != undefined){
                        editor.txt.html(ret['content']);
                    }
                    if(ret['file_path'] != '' && ret['file_path'] != null  && ret['file_path'] != undefined ){
                        $("#attach").attr('src',ret['attach_id']);
                        $("#file_name").html(ret['file_name']);
                    } else {
                        $(".attach_p").css("display","none");
                    }
                    
                   
                } else {
                    layer.msg(ret.msg,{icon:3});
                }
            }
        });
        
    });
    $("#cat_id_1").click(function(){
        var _this = $(this);
        var htl = '<option value="0">{:__("请选择子行业")}</option>';
        var mission = getLocalStorage("mission_category");
        var mission_category = JSON.parse(mission);
        var cat_id_1 = _this.val();
        if(cat_id_1 == 0){
            $("#cat_id").html(htl);
            return false;
        }
        cat_id_1 = parseInt(cat_id_1);
        var _child = mission_category[cat_id_1]['_child'];
        for(i in _child){
            htl += "<option value='"+_child[i]['id']+"'>"+_child[i]['name']+"</option>";
        }
        $("#cat_id").html(htl);
    });
    // 获取分类及获取编辑
    $(document).dequeue('task_class');
    
    $("#click_enclosure").click(function(){
        if($(".attach_p").css("display") == 'none'){
            $(".attach_p").css("display","flex");
        } else {
            $(".attach_p").css("display","none");
        }
    });
    $("#attach_sub").click(function(){
        var formdata = new FormData();
        var files = $("#attach")[0].files[0];
        if(files == '' || files == null || files == undefined ){
            layer.msg("{:__('请选择文件')}",{icon:2});
            $("#edit_task").removeClass("disabled");
            $("#edit_task").addClass("layui-btn-primary");
            $("#edit_task").removeClass("layui-btn-disabled");
            return false;
        }
        formdata.append("file",files);
        formdata.append("token",token);
        if(files['type'].indexOf('image')!=-1){
            formdata.append("file_type",'image');
        } else {
            formdata.append("file_type",'file');
        }
        $.ajax({
            url:'/api/common/upload',
            type:'post',
            data:formdata,
            processData:false,
            contentType:false,
            success:function(ret){
                if(ret.code != 401 && ret.code != 1){
                    layer.msg(ret.msg,{icon:2},function(){
                        $("#edit_task").removeAttr("disabled");
                        $("#edit_task").addClass("layui-btn-primary");
                        $("#edit_task").removeClass("layui-btn-disabled");
                    });
                } else if(ret.code ==1){
                    layer.msg(ret.msg,{icon:1},function(){
                        if(ret['data']['file'] != '' && ret['data']['file'] != null && ret['data']['file'] != undefined){
                            $("#attach").attr("src",ret['data']['file']['attach_id']);
                            $("#edit_task").removeAttr("disabled");
                            $("#edit_task").addClass("layui-btn-primary");
                            $("#edit_task").removeClass("layui-btn-disabled");
                        }
                    });
                } else {
                    $("#edit_task").removeAttr("disabled");
                    $("#edit_task").addClass("layui-btn-primary");
                    $("#edit_task").removeClass("layui-btn-disabled");
                }
            },error:function(){
                $("#edit_task").removeAttr("disabled");
                $("#edit_task").addClass("layui-btn-primary");
                $("#edit_task").removeClass("layui-btn-disabled");
            }
        });
    });
    $("#edit_task").click(function(){
        var title = $("#title").val();
        var cat_id = $("#cat_id").val();
        var content = editor.txt.html();
        var attach = $("#attach").attr("src");
        if(title == '' || title == null || title == undefined){
            layer.msg("{:__('任务标题不能为空')}",{icon:3});
            return false;
        }
        if(cat_id <= 0){
            layer.msg("{:__('任务分类子行业不能为空')}",{icon:3});
            return false;
        }
        if(content == '' || content == null || content == undefined){
            layer.msg("{:__('任务描述不能为空')}",{icon:3});
            return false;
        }
        $("#attach_copy").attr("disabled",true);
        $("#attach_copy").addClass("layui-btn-disabled");
        $("#attach_copy").removeClass("layui-btn-primary");
        $.ajax({
            url:'/api/mission/update',
            type:'post',
            data:{'token':token,'title':title,'cat_id':cat_id,'content':content,'attach_id':attach,'mission_id':"{$id}"},
            success:function(ret){
                if(ret.code == 1){
                    layer.msg(ret.msg,{icon:1},function(){
                        parent.location.reload();
                    });
                } else if(ret.code !=401){
                    layer.msg(ret.msg,{icon:2},function(){
                        $("#attach_copy").removeAttr("disabled");
                        $("#attach_copy").removeClass("layui-btn-disabled");
                        $("#attach_copy").addClass("layui-btn-primary");
                    });
                }
            }
        })
    });
    $("#attach_copy").click(function(){
        $("#attach").val("");
        $("#attach").trigger("click");
    });
    $("#attach").change(function(){
        $("#edit_task").attr("disabled",true);
        $("#edit_task").removeClass("layui-btn-primary");
        $("#edit_task").addClass("layui-btn-disabled");
        var filename = $("#attach")[0].files[0].name;
        $("#file_name").html(filename);
        $("#attach_sub").trigger("click");
    });

</script>